package com.android.hedgehog.puzzles;

import java.util.Random;

public class GoalieEngine2 {
	Random rand = new Random();
	public int difficulty;
	public Block2 block;
	public Puck2 puck;
	String testMsg = "";
	String testMsg2 = "";
	boolean deflected = false;

	public GoalieEngine2(int difficulty) {
		this.difficulty = difficulty;
		configure();
	}

	private void configure() {
		int dx = 0;
		int dy = 0;
		double slope = 0;
		int wid = 30;

		do {// UNIVERSAL RESTRAINTS

			while (true) {// LEVEL RESTRAINTS

				dx = -250 + rand.nextInt(501);
				dy = -250 + rand.nextInt(501);
				slope = (double) dy / (double) dx;

				if (difficulty < 3 && Math.abs(slope) > 1.9 && Math.abs(slope) < 2.2 && dy > 0) {// 1-2
					if (difficulty == 1) {
						wid = 60;
						break;
					}
					if (difficulty == 2) {
						wid = 40;
						break;
					}
				}// 1-2

				if ((difficulty == 3 || difficulty == 4) && Math.abs(slope) > 1.4 && Math.abs(slope) < 1.8
						&& dy > 0) {// 3-4
					if (difficulty == 3) {
						if (slope > 1.72)
							wid = 48;
						else if (slope > 1.6)
							wid = 53;
						else
							wid = 60;
						break;
					}
					if (difficulty == 4) {
						if (slope > 1.7)
							wid = 33;
						else
							wid = 40;
						break;
					}

				}// 3-4

				if ((difficulty == 5 || difficulty == 6) && Math.abs(slope) > 1 && Math.abs(slope) < 1.40
						&& dy > 0) {// 5-6
					if (difficulty == 5) {
						if (slope > 1.31)
							wid = 48;
						else
							wid = 54;
						break;
					}
					if (difficulty == 6) {
						if (slope > 1.32)
							wid = 33;
						else
							wid = 40;
						break;
					}
				}// 5-6

				if (difficulty == 7 || difficulty == 8) {// 7-8

					if (Math.abs(slope) > .75 && Math.abs(slope) < 1 && dy > 0) {
						if (difficulty == 7) {
							if (slope > .93)
								wid = 49;
							else
								wid = 54;
							break;
						}
						if (difficulty == 8) {
							if (slope > .93)
								wid = 33;
							else
								wid = 40;
							break;
						}
					}

					if (Math.abs(slope) > 2.5 && Math.abs(slope) < 4 && dy < 0) {
						if (difficulty == 7) {
							if (slope > 3.7)
								wid = 42;
							else if (slope > 3)
								wid = 45;
							else
								wid = 50;
							break;
						}

						if (difficulty == 8) {
							if (slope > 3.7)
								wid = 31;
							else if (slope > 3)
								wid = 34;
							else
								wid = 39;
							break;
						}

					}

				}// 7-8

				if (difficulty == 9 || difficulty == 10) {// 9-10

					if (Math.abs(slope) > .60 && Math.abs(slope) < .75 && dy > 0) {
						if (difficulty == 9) {
							if (slope > .71)
								wid = 49;
							else
								wid = 54;
							break;
						}
						if (difficulty == 10) {
							if (slope > .71)
								wid = 33;
							else
								wid = 40;
							break;
						}
					}

					if (Math.abs(slope) > 1.3 && Math.abs(slope) < 3.3 && dy < 0) {
						if (difficulty == 9) {
							if (slope > 3)
								wid = 38;
							else if (slope > 2.2)
								wid = 44;
							else
								wid = 47;
							break;
						}

						if (difficulty == 10) {
							if (slope > 3.1)
								wid = 30;
							else if (slope > 2.2)
								wid = 36;
							else
								wid = 40;
							break;
						}

					}

				}// 9-10

			}// LEVEL RESTRAINTS

		} while (Math.abs(dy) < 30 || Math.abs(dx) + Math.abs(dy) < 100);// RESTRAINTS

		float xPos = 100.0f + rand.nextFloat() * 120;
		if (Math.abs((float) 160 - xPos) < 30)
			wid -= 2;

		testMsg = "D: " + difficulty + " dx: " + dx + "  dy: " + dy + " xPos: " + (int)xPos + " w: " + wid
				+ " Slope: " + slope;
		block = new Block2(190, 340, wid, 30);
		puck = new Puck2(xPos, 60.0f + rand.nextFloat() * 80, dx, dy);

	}

	public boolean checkCollision() {

		if (puck.y2 > block.y && puck.y2 < block.y2 && (puck.y + 20) < block.y) {// BLOCKED

			if (puck.x > block.x && puck.x < block.x2) {
				puck.dirY = -puck.dirY; // bounce back R->L
				testMsg = "bounce back R->L";
				return true;
			}
			if (puck.x2 > block.x && puck.x2 < block.x2) {
				puck.dirY = -puck.dirY;// bounce back L->R
				testMsg = "bounce back L->R";
				return true;
			}

		}// BLOCKED// front part of puck overlapping block

		if (puck.y2 > block.y && puck.y2 < block.y2 || puck.y > block.y && puck.y < block.y2) {

			if (puck.x < block.x2 && puck.x > block.x) {
				puck.dirX = -puck.dirX;// bounce RIGHT;
				testMsg = "bounce right";
				deflected = true;
				return false;

			}// if puck overlaps right side of block

			if (puck.x2 < block.x2 && puck.x2 > block.x) {
				puck.dirX = -puck.dirX;// bounce LEFT;
				testMsg = "bounce left";
				deflected = true;
				return false;

			}// if puck overlaps left side of block

		}// BOUNCED if part of puck overlapping block in Ypos

		return false;
	}

	public boolean checkGoal() {
		if (puck.y > 451) {
			return true;
		}
		return false;
	}

}
